import json
import threading
import time
from datetime import datetime, timedelta
from tools.redis_server import RedisServer

# 106.120.201.126,, 14611
redis_server = RedisServer('10.102.1.113', 6379, 0, 'Wanji@300552!')
channel = 'ceshi001'


def send_data():
    while True:
        redis_server.publish(channel, str(time.time()))
        time.sleep(0.1)


def get_data():
    while True:
        redis_server.subscribe(channel)
        a = redis_server.get_messages()
        print(time.time(), len(a))
        time.sleep(0.1)


def main():
    # log_path = r'/home/gj/Download/Log/卡口相机.log'
    log_path = r'/home/gj/Download/新建文件夹/kako_data_ceshi.log'
    t1 = datetime.strptime("2024-12-24T15:18:10.625", "%Y-%m-%dT%H:%M:%S.%f")
    with open(log_path, 'r', encoding='utf-8') as f:
        current_time = None
        while True:
            message = f.readline()
            if not message:  # 如果文件读取完毕，则退出循环
                break
            try:
                msg = json.loads(message.strip())
                t = datetime.strptime(msg["timestamp"], "%Y-%m-%dT%H:%M:%S.%f")
                # if t < t1:
                #     continue
                if current_time is not None:
                    sleep_duration = (t - current_time).total_seconds()
                    if sleep_duration > 0:
                        # print("当前时间:", t, " 跳过:", sleep_duration)
                        time.sleep(sleep_duration)
                        # time.sleep(0.1)
                current_time = t

                if True:
                    redis_server.publish(channel, message)
                    print(t, msg)

            except json.JSONDecodeError:
                print("忽略非JSON格式的日志行")
                continue


if __name__ == '__main__':
    # threading.Thread(target=get_data).start()
    # get_data()

    main()

